package gw.com.android.presenter.http;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigUtil;
import gw.com.android.utils.CacheUtils;
import okhttp3.Request;
import okhttp3.Response;
import www.com.library.app.Logger;
import www.com.library.interfaces.ReqCallBack;

/**
 * 交易风向标的请求
 *
 * @author hank
 * @since 2018-8-2
 */
public class HomeTradeVaneRequest {

    private String cacheFileName = "_trade_vane.dat";

    public HomeTradeVaneData fetchLocal() {
        String str = CacheUtils.readFile(cacheFileName);
        return parse(str, false);
    }

    public void call(final ReqCallBack<HomeTradeVaneData> callBack) {
        String mUrl = ConfigUtil.instance().getUrlPath("tradeVane");
        new Https(mUrl).responseOnUiThread(false).get(new Https.ResponseCallback<String>() {
            @Override
            public void onSuccess(Request request, Response response, String result, int code) {
                HomeTradeVaneData data = parse(result, true);
                postSuccess(data, callBack);
            }

            @Override
            public void onFailure(Request request, IOException e) {
                postFailure(e, callBack);
            }
        });
    }

    private void postSuccess(final HomeTradeVaneData data, final ReqCallBack<HomeTradeVaneData> callBack) {
        AppMain.getApp().getHandler().post(new Runnable() {
            @Override
            public void run() {
                callBack.onReqSuccess(data);
            }
        });
    }

    private void postFailure(Exception e, final ReqCallBack<HomeTradeVaneData> callBack) {
        final String msg = e == null ? "" : e.getMessage();
        AppMain.getApp().getHandler().post(new Runnable() {
            @Override
            public void run() {
                callBack.onReqFailed(msg);
            }
        });
    }

    private void storeLocal(String str) {
        CacheUtils.writeFile(cacheFileName, str);
    }

    private HomeTradeVaneData parse(String str, boolean cache) {
        Log.d("HomeTradeVaneData", "" + str);
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(str);
            JSONObject dataJson = jsonObject.optJSONObject("data");
            if (dataJson != null) {
                HomeTradeVaneData data = new HomeTradeVaneData();
                //先解析多空
                if (dataJson.has("hx")) {
                    JSONObject hx = dataJson.optJSONObject("hx");
                    data.goldBuyPercent = hx.optDouble("goldBuyPercent");
                    data.goldSellPercent = hx.optDouble("goldSellPercent");
                    data.silverBuyPercent = hx.optDouble("silverBuyPercent");
                    data.silverSellPercent = hx.optDouble("silverSellPercent");
                    data.lastestTime = hx.optString("lastestTime");
                }

                if (dataJson.has("openclose")) {
                    JSONObject openclose = dataJson.optJSONObject("openclose");
                    data.goldOpenPercent = openclose.optDouble("goldOpenPercent");
                    data.goldClosePercent = openclose.optDouble("goldClosePercent");
                    data.silverOpenPercent = openclose.optDouble("silverOpenPercent");
                    data.silverClosePercent = openclose.optDouble("silverClosePercent");
                    data.lastestTime = openclose.optString("lastestTime");
                }
                if (cache) {
                    storeLocal(str);
                }
                return data;
            }
        } catch (JSONException e) {
            Logger.e(e);
        }
        return null;
    }

    public class HomeTradeVaneData {
        public String lastestTime;//最新更新时间
        public double goldBuyPercent;//float 伦敦金看多百分比
        public double goldSellPercent;//float 伦敦金看空百分比
        public double silverBuyPercent;// float 伦敦银看多百分比
        public double silverSellPercent;//float 伦敦银看空百分比

        public double goldOpenPercent;//float 伦敦金持仓百分比
        public double goldClosePercent;//float 伦敦金平仓百分比
        public double silverOpenPercent;//float 伦敦银持仓百分比
        public double silverClosePercent;//float 伦敦银平仓百分比

    }

}
